package com.nova.edc.service.system.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.nova.edc.dao.system.CollectAreaItemDao;
import com.nova.edc.model.system.CollectAreaItem;
import com.nova.edc.service.system.CollectAreaItemService;
import com.nova.frame.dao.buzz.query.Order;
import com.nova.frame.dao.buzz.query.Query;
import com.nova.frame.dao.criterion.Restrictions;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.JdbcUtils;
import com.nova.module.filter.query.Filter;

public class CollectAreaItemServiceImpl implements CollectAreaItemService {

	@Autowired
	private CollectAreaItemDao novaCollectAreaItemDao;

	@Override
	public List<CollectAreaItem> findByHeadId(int headId) {
		return novaCollectAreaItemDao.query()
				.add(Restrictions.eq("t_collectarea_item.HeadID", headId))
				.list();
	}

	@Override
	public int insert(List<CollectAreaItem> areaItems) {
		return novaCollectAreaItemDao.insert(areaItems);
	}

	@Override
	public void update(List<CollectAreaItem> areaItems) {
		novaCollectAreaItemDao.updateNotNull(areaItems);
	}

	@Override
	public CollectAreaItem insert(CollectAreaItem collectAreaItem) {
		return novaCollectAreaItemDao.insert(collectAreaItem);
	}

	@Override
	public int update(CollectAreaItem collectAreaItem) {
		return novaCollectAreaItemDao.updateNotNull(collectAreaItem);
	}

	@Override
	public int delete(Integer id) {
		return novaCollectAreaItemDao.deleteById(String.valueOf(id));
	}

	@Override
	public List<CollectAreaItem> findByLightType(int headId, int lightType) {
		return novaCollectAreaItemDao
				.query()
				.add(Restrictions.eq("t_collectarea_item.HeadID", headId))
				.add(Restrictions.eq("t_collectarea_item.LightType", lightType))
				.list();
	}

	@Override
	public CollectAreaItem getByLightType(int headId, int lightType) {
		return novaCollectAreaItemDao
				.query()
				.add(Restrictions.eq("t_collectarea_item.HeadID", headId))
				.add(Restrictions.eq("t_collectarea_item.LightType", lightType))
				.get();
	}

	@Override
	public PageList<CollectAreaItem> findCollectAreaItemPageList(
			CollectAreaItem collectAreaItem, Page page, String sidx, String sord) {
		Query<CollectAreaItem> query = novaCollectAreaItemDao.query().addOrder(
				Order.custom(sord, sidx));
		Filter.addFilterCondition(query, collectAreaItem);
		return query.pageList(page);
	}

	@Override
	public float getTraLightPower(int companyCode) {
		String sql = "select sum(t_device_light.Power*t_collectarea_item.TraNum) trapower from t_collectarea_item"
				+ " left join t_device_light"
				+ " on t_collectarea_item.TraLightID = t_device_light.ID"
				+ " where t_collectarea_item.CompanyCode=?";
		Map<String, Object> result = JdbcUtils.getMap(sql, companyCode);
		Float power = result.get("trapower") == null ? 0 : Float.valueOf(result
				.get("trapower").toString());
		return power;
	}

	@Override
	public float getLedLightPower(int companyCode) {
		String sql = "select sum(t_device_light.Power*t_collectarea_item.LedNum) ledpower from t_collectarea_item"
				+ " left join t_device_light"
				+ " on t_collectarea_item.LedLightID = t_device_light.ID"
				+ " where t_collectarea_item.CompanyCode=?";
		Map<String, Object> result = JdbcUtils.getMap(sql, companyCode);
		Float power = result.get("ledpower") == null ? 0 : Float.valueOf(result
				.get("ledpower").toString());
		return power;
	}

	@Override
	public List<CollectAreaItem> findByCompanyCode(
			CollectAreaItem collectAreaItem, String sdix, String sord) {
		Query<CollectAreaItem> query = novaCollectAreaItemDao.query();
		query.addOrder(Order.custom(sord, sdix));
		Filter.addFilterCondition(query, collectAreaItem);
		return query.list();
	}

	@Override
	public List<CollectAreaItem> findByCompanyCode(List<Integer> companyCodes,
			String sord, String sdix) {
		Query<CollectAreaItem> query = novaCollectAreaItemDao.query();
		query.add(Restrictions.in("t_collectarea_item.CompanyCode",
				companyCodes));
		return query.list();
	}
}
